
package org.okkam.refine.strategy;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.okkam.client.data.AttributeMetadataType;
import org.okkam.client.data.AttributeType;
import org.okkam.client.data.AttributesType;
import org.okkam.client.data.Entity;
import org.okkam.client.data.ProfileType;
import org.okkam.client.util.XMLEntityConverter;
import org.okkam.refine.strategy.CandidateStrategy;
import org.okkam.refine.strategy.OtherCandidate;

public class OtherCandidate implements CandidateStrategy {

    private static Log log = LogFactory.getLog(OtherCandidate.class);

    @Override
    public boolean checkEquality(List<AttributeType> attributeTypeList, List<String> entityList) {

        Iterator<AttributeType> attributeTypeIterator = attributeTypeList.iterator();
        Map<String, String> propValueNewEntity = new HashMap<String, String>();
        boolean match = false;

        while (attributeTypeIterator.hasNext()) {
            AttributeType attributeType = attributeTypeIterator.next();
            String term = attributeType.getName().getLocalPart().toLowerCase();
            String value = attributeType.getValue().toLowerCase().replace("\"", "");
            AttributeMetadataType metadata = attributeType.getMetadata();
            propValueNewEntity.put(term, value);
        }

        Iterator entityIterator = entityList.iterator();
        while (entityIterator.hasNext()) {
            XMLEntityConverter converter = new XMLEntityConverter();
            Entity entity = null;
            try {
                entity = converter.xmlToEntity(entityIterator.next().toString());
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ProfileType profile = entity.getProfile();
            AttributesType attributesType = profile.getAttributes();
            List<AttributeType> attributeTypeListResponse = attributesType.getAttributes();
            Iterator<AttributeType> attributeTypeIteratorresponse = attributeTypeListResponse.iterator();

            Map<String, String> propValueResponse = new HashMap<String, String>();

            while (attributeTypeIteratorresponse.hasNext()) {
                AttributeType attributeType = attributeTypeIteratorresponse.next();
                String term = attributeType.getName().getLocalPart().toLowerCase();
                String value = attributeType.getValue().toLowerCase().replace("\"", "");
                propValueResponse.put(term, value);
            }

            // based on company name, codice Fiscale of the company (Owner)...,
            // ...
            if (propValueResponse.containsKey("organization_legal_name") 
                    && (propValueResponse.containsKey("codice_fiscale") 
                     || propValueResponse.containsKey("partita_iva"))) {

                ArrayList<String> nameChecks = new ArrayList<String>();
               
                if (propValueResponse.containsKey("organization_legal_name"))
                    nameChecks.add(propValueResponse.get("organization_legal_name"));

                if (propValueResponse.containsKey("codice_fiscale"))
                    nameChecks.add(propValueResponse.get("codice_fiscale"));

                if (propValueResponse.containsKey("partita_iva"))
                    nameChecks.add(propValueResponse.get("partita_iva"));

                Iterator itNameCheck = nameChecks.iterator();

                while (itNameCheck.hasNext()) {
                    match = false;
                    String valOfResponse = (String) itNameCheck.next();

                    Iterator itq = propValueNewEntity.keySet().iterator();

                    while (itq.hasNext()) {
                        String valOfNewEntity = propValueNewEntity.get(itq.next()).replace("\"", "");
                        if (valOfResponse.toLowerCase().equals(valOfNewEntity.toLowerCase())) {
                            match = true;
                            break;
                        }
                    }
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    return match;
                }
            }
        }
        return match;
    }
}
